package com.produtos;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;
	public static final String TABLE_NAME = "produtos";
	private static final String DATABASE_NAME = "lista.db";
	
	public static final String ID = "_id";
	public static final String CODE = "code";
	public static final String PRODUTO = "produto";
	public static final String DESC = "desc";
	public static final String PRECO = "preco";
	public static final String QTD = "qtd";	
	public static final String FOTO = "foto";
	
	public static final String TABLE_LISTA_DE_COMPRAS = "lista_de_compras";
	public static final String ID_COMPRA = "id_compra";
	public static final String ID_PRODUTO = "id_produto";
	public static final String DATA = "data";
	public static final String LOCAL = "local";
	public static final String EMPRESA = "empresa";
	
	private static final String DATABASE_CREATE = "create table "
			+ TABLE_NAME + "( " 
			+ ID + " integer primary key autoincrement, "
			+ CODE + " text not null, "
			+ PRODUTO + " text not null, " 
			+ DESC + " text not null, "
			+ QTD + " text not null, "
			+ PRECO+"  text not null" + ", "+ 
			FOTO  +" BLOB);";
	
	private static final String DATABASE_CREATE_LISTA_DE_COMPRA = "create table "
			+ TABLE_LISTA_DE_COMPRAS + "( " 
			+ ID_COMPRA  + " integer primary key autoincrement, "
			+ ID_PRODUTO + " integer not null, "
			+ DATA    + " date, " 
			+ LOCAL   + " text , "
			+ EMPRESA +"  text , "
			+ "Foreign key (" + ID_PRODUTO+ ") references " + TABLE_NAME + "(" + ID + ")"
			+ " );";
	
	public DbHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		//super()
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
		db.execSQL(DATABASE_CREATE_LISTA_DE_COMPRA);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DbHelper.class.getName(), "Upgrading database from version " + oldVersion + " to "	+ newVersion + ", which will destroy all old data");
	db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}


}
